/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package master.proyectoweb.ejb;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import master.proyectoweb.entities.User;

/**
 *
 * @author joaquin
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {
    @PersistenceContext(unitName = "TurnItUpPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }
    
    public User editUser(User entity) {
        User user = null;
        try{
            user = (User) getEntityManager().find(User.class, entity.getId());
        
            user.setName(entity.getName());
            user.setSurname(entity.getSurname());
            user.setPassword(entity.getPassword());
            user.setPhone(entity.getPhone());
            user.setAddress(entity.getAddress());
            user.setCountry(entity.getCountry());
            user.setCity(entity.getCity());
            user.setPostalCode(entity.getPostalCode());
        }catch(Exception e){          
        }
        
        return user;
    }
 
    public User findByEmailAndPassword(String email, String password) {
        User user = null;
        Query query = em.createNamedQuery("User.findByEmailAndPassword");
        
        query.setParameter("email", email);
        query.setParameter("password",password);
        try{
            user = (User) query.getSingleResult();
        }catch(NoResultException e){}
        
        return user;        
    }   
}
